package com.juvepoland.jpdroid.activities;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.juvepoland.jpdroid.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

public class MyPreferencesActivity extends PreferenceActivity {
	
	protected static final String MY_PREFERENCES = "jpDroidMyPreferences";
	protected static final String PREFERENCE_NAME_DISPLAY_NEWS_IMAGES = "display_news_images";
	protected static final String PREFERENCE_NAME_DISPLAY_AVATARS = "display_avatars";
	protected static final String PREFERENCE_NAME_NEWS_COUNT_PER_SINGLE_PAGE = "news_count_per_single_page";
	protected static final String PREFERENCE_NAME_MAX_PAGE_LOAD_CLICKED_COUNT = "max_page_load_clicked_count";
	protected static final String PREFERENCE_DEFAULT_FONT_NAME = "font_name_preferences";
	
	private Properties properties;
	private SharedPreferences mySharedPreferences;
	private InputStream rawResource;
	
	private CheckBoxPreference displayNewsImagesCheckBoxPreference;
	private CheckBoxPreference displayAvatarsCheckBoxPreference;
	
	private ListPreference fontNamePreference;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.preferences);
		mySharedPreferences = getSharedPreferences(MY_PREFERENCES, Activity.MODE_PRIVATE);
		displayNewsImagesCheckBoxPreference = (CheckBoxPreference)findPreference(PREFERENCE_NAME_DISPLAY_NEWS_IMAGES);
		displayAvatarsCheckBoxPreference = (CheckBoxPreference)findPreference(PREFERENCE_NAME_DISPLAY_AVATARS);
		fontNamePreference = (ListPreference)findPreference(PREFERENCE_DEFAULT_FONT_NAME);
		try {
			rawResource = this.getResources().openRawResource(R.raw.jpdroid);
			properties = new Properties();
			properties.load(rawResource);			
		} catch (IOException e) {
			Toast.makeText(getApplicationContext(), "Błąd ładowania pliku properties! " + e.getMessage(),
					Toast.LENGTH_LONG).show();
			finish();
		}
		
		
		getPreferences();
	
	}
	
	private void test() {
		Log.d(PREFERENCE_NAME_DISPLAY_AVATARS, String.valueOf(displayAvatarsCheckBoxPreference.isChecked()));
		Log.d(PREFERENCE_NAME_DISPLAY_NEWS_IMAGES, String.valueOf(displayNewsImagesCheckBoxPreference.isChecked()));
		
	}

	protected void getPreferences() {
		displayNewsImagesCheckBoxPreference.setChecked(mySharedPreferences.getBoolean(PREFERENCE_NAME_DISPLAY_NEWS_IMAGES, Boolean.parseBoolean(properties.getProperty(PREFERENCE_NAME_DISPLAY_NEWS_IMAGES))));
		displayAvatarsCheckBoxPreference.setChecked(mySharedPreferences.getBoolean(PREFERENCE_NAME_DISPLAY_AVATARS, Boolean.parseBoolean(properties.getProperty(PREFERENCE_NAME_DISPLAY_AVATARS))));
		
		fontNamePreference.setValue(mySharedPreferences.getString(PREFERENCE_DEFAULT_FONT_NAME, properties.getProperty("basicNormalFont", "Oswald.ttf")));
	}
	
	@Override
    protected void onStop() {
        setPrefereces();
       // test();
        super.onStop();
    }

	private void setPrefereces() {
		
		SharedPreferences.Editor editor = mySharedPreferences.edit();
		editor.putBoolean(PREFERENCE_NAME_DISPLAY_NEWS_IMAGES, displayNewsImagesCheckBoxPreference.isChecked());
		editor.putBoolean(PREFERENCE_NAME_DISPLAY_AVATARS, displayAvatarsCheckBoxPreference.isChecked());
		editor.putString(PREFERENCE_DEFAULT_FONT_NAME, fontNamePreference.getValue());
		
		editor.commit();
	}
	
}
